package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageGetBatchRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialBatchBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStoragePurchaseOrderBean;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageProductionOverviewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/29 15:38
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageUtils {

    public static final String CHANGE_STORAGE_TYPE = "4";//转储
    //获取转储信息
    public static CYChangeStorageBean getChangeStorageBean(Context context) {
        String json = SPUtils.getChangeStorageInformation(context);
        if (TextUtils.isEmpty(json)) {
            return new CYChangeStorageBean();
        } else {
            Gson gson = new Gson();
            return gson.fromJson(json, CYChangeStorageBean.class);
        }
    }
    //保存转储信息
    public static void saveCYChangeStorageInformation (Context context , CYChangeStorageBean bean) {
        Gson gson = new Gson();
        String json = gson.toJson(bean, CYChangeStorageBean.class);
        SPUtils.saveChangeStorageInformation(context, json);
    }
    //保存采购订单信息
    public static void savePurchaseOrder(Context context , List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> list) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        bean.setPurchaseOrderBean(list);
        saveCYChangeStorageInformation(context , bean);
    }
    //保存物料信息
    public static void saveMaterialList(Context context , List<ItemListBean> list) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        bean.setMaterialBeanList(list);
        saveCYChangeStorageInformation(context , bean);
    }
    //保存库存地点信息
    public static void saveLocation(Context context , String locationCode , String locationName , String locationId) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        bean.setLocationCode(locationCode);
        bean.setLocationName(locationName);
        bean.setLocationId(locationId);
        saveCYChangeStorageInformation(context , bean);
    }

    //获取采购订单list
    public static List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> getPurchaseOrderList (Context context) {
        List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> purchaseOrderBeanList;
        CYChangeStorageBean changeStorageBean;
        changeStorageBean = getChangeStorageBean(context);
        if (changeStorageBean.getPurchaseOrderBean() == null || changeStorageBean.getPurchaseOrderBean().size() == 0) {
            purchaseOrderBeanList = new ArrayList<>();
        }else {
            purchaseOrderBeanList = changeStorageBean.getPurchaseOrderBean();
        }
        return purchaseOrderBeanList;
    }
    /**
     *  获取去重之后的采购订单列表
     * @param addList 新增加的采购订单
     * @return
     */
    public static List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> getCYChangeStoragePurchaseOrderBean(Context context ,List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> addList){
        List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> list = getChangeStorageBean(context).getPurchaseOrderBean();
        //先把先添加的全部加到原来的list中
        for (int i = 0; i < addList.size(); i++) {
            list.add(addList.get(i));
        }
        //去重
        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size()-1; j > i; j--) {
                if (list.get(i).getPurchase_orde_code().equals(list.get(j).getPurchase_orde_code())) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    /**
     *  删除采购订单行项目
     * @param context
     * @param purchaseOrderNumber
     */
    public static void deletePurchaseOrder (Context context , String purchaseOrderNumber) {
        List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> list = getChangeStorageBean(context).getPurchaseOrderBean();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPurchase_orde_code().equals(purchaseOrderNumber)) {
                list.remove(i);
            }
        }
        savePurchaseOrder(context , list);
    }

    /**
     * 获取所有物料
     * @param context
     * @return
     */
    public static List<ItemListBean> getAllMaterialList (Context context) {
       return getAllMaterial(context).getBody();

    }
    //获取所有物料bean
    public static CYChangeStorageMaterialListBean getAllMaterial (Context context) {
        String material = SPUtils.getChangeStorageMaterielList(context);
        Gson gson = new Gson();
        CYChangeStorageMaterialListBean bean = gson.fromJson(material , CYChangeStorageMaterialListBean.class);
        return bean;
    }
    //保存所有物料
    public static void saveCYChangeStorageMaterialInformation (Context context , CYChangeStorageMaterialListBean bean) {
        Gson gson = new Gson();
        String json = gson.toJson(bean, CYChangeStorageMaterialListBean.class);
        SPUtils.saveChangeStorageMaterielList(context, json);
    }
    /**
     *  通过物料编码获取物料行项目
     * @aram context
     * @param materialCode
     */
    public static List<ItemListBean> getMaterialItem (Context context , String materialCode) {
        List<ItemListBean> allData = getAllMaterialList(context);
        List<ItemListBean> list = new ArrayList<>();
        if (TextUtils.isEmpty(materialCode)) {
            return allData;
        }
        for (int i = 0; i < allData.size(); i++) {
            if (allData.get(i).getMat_code().equals(materialCode)) {
                list.add(allData.get(i));
            }
        }
        return list;
    }

    //更新领料出库详情
    public static void updateProductionOutStorageDetail(Context context, ItemListBean materielItem) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        List<ItemListBean> productionOutStorageList = bean.getMaterialBeanList();
        for (int i = 0; i < productionOutStorageList.size(); i++) {
            ItemListBean pickingOutStorageItem = productionOutStorageList.get(i);
            if (materielItem.getMat_id().equals(pickingOutStorageItem.getMat_id()) && materielItem.getSupplier_code().equals(pickingOutStorageItem.getSupplier_code())) {
                productionOutStorageList.remove(pickingOutStorageItem);
                break;
            }
        }
        productionOutStorageList.add(0, materielItem);
        bean.setMaterialBeanList(productionOutStorageList);
        saveCYChangeStorageInformation(context, bean);
//        CYChangeStorageMaterialListBean bean1 = getAllMaterial(context);
//        bean1.setBody(productionOutStorageList);
//        saveCYChangeStorageMaterialInformation(context , bean1);
    }
    /**
     * 通过物料id和供应商code获取物料信息不田间批次list
     * @param context
     * @param matId
     * @param supplierCode
     * @param add
     * @return
     */
    public static ItemListBean getMaterialDetailById (Context context , String matId , String supplierCode ,boolean add) {
        return getMaterialDetailById(context , matId , supplierCode , null , add);
    }
    /**
     * 通过物料id和供应商code获取物料信息并添加批次list
     * @param context
     * @param matId
     * @param supplierCode
     * @param batchBeanList
     * @param add
     * @return
     */
    public static ItemListBean getMaterialDetailById (Context context , String matId , String supplierCode , List<BatchBean.BatchInfoBean> batchBeanList , boolean add) {
        int position = 0;
        CYChangeStorageBean changeStorageBean = getChangeStorageBean(context);
        ItemListBean bean = new ItemListBean();
        List<ItemListBean> materialList = changeStorageBean.getMaterialBeanList();
        for (int i = 0; i < materialList.size(); i++) {
            if (materialList.get(i).getMat_id().equals(matId) && materialList.get(i).getSupplier_code().equals(supplierCode)) {
                position = i;
                bean = materialList.get(i);
                break;
            }
        }
        if (batchBeanList != null) {
            if (add) {
                List<BatchBean.BatchInfoBean> list = bean.getBatchList() == null ? new ArrayList<>() : bean.getBatchList();
                list.addAll(batchBeanList);
                //去重
                for (int i = 0; i < batchBeanList.size(); i++) {
                    for (int j = batchBeanList.size()-1; j > i; j--) {
                        if (list.get(i).getBatch().equals(list.get(j).getBatch())) {
                            list.remove(j);
                        }
                    }
                }
            }
            bean.setBatchList(batchBeanList);
            materialList.set(position , bean);
            changeStorageBean.setMaterialBeanList(materialList);
            saveCYChangeStorageInformation(context ,changeStorageBean);
        }
        return bean;
    }

    /**
     * 拼接获取批次bean
     * @param matId
     * @param batch
     * @return
     */
    public static CYChangeStorageGetBatchRequestBean getBatchRequestBean (Context context , String matId, String batch) {
        CYChangeStorageBean changeStorageBean = getChangeStorageBean(context);
        String locationId = changeStorageBean.getLocationId();
        CYChangeStorageGetBatchRequestBean bean = new CYChangeStorageGetBatchRequestBean();
        bean.setMat_id(matId);
        bean.setLocation_id(locationId);
        bean.setBatch(batch);
        return bean;
    }
    //获取库存地点
    public static String getLocation (Context context) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        return bean.getLocationCode() + "-" + bean.getLocationName();
    }

    //检查转储管理物料列表是否至少填写一项
    public static int getChangeStorageMaterielNumber(Context context) {
        int result = 0;
        List<ItemListBean> materielList = getChangeStorageBean(context).getMaterialBeanList();
        if (materielList == null || materielList.isEmpty()) {
            return result;
        }
        for (int i = 0; i < materielList.size(); i++) {
            ItemListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getActualSendNumber()) && Float.parseFloat(materielItem.getActualSendNumber()) > 0) {
                result = result + 1;
            }
        }
        return result;
    }

    //拼接转储提交bean
    public static CYChangeStorageConfirmRequestBean getChangeStorageConfirmRequestBean (Context context) {
        CYChangeStorageConfirmRequestBean bean = new CYChangeStorageConfirmRequestBean();
        CYChangeStorageBean changeStorageBean = getChangeStorageBean(context);
        bean.setStock_transport_id(TextUtils.isEmpty(changeStorageBean.getStock_transport_id()) ? "0" : changeStorageBean.getStock_transport_id());
        bean.setLocation_id(changeStorageBean.getLocationId());
        List<CYChangeStorageConfirmRequestBean.ItemListBean> itemList = new ArrayList<>();
        List<ItemListBean> list = changeStorageBean.getMaterialBeanList();
        for (int i = 0; i < list.size(); i++) {
//            LogUtils.i("ActualSendNumber ==== " + list.get(i).getActualSendNumber());
            if (!TextUtils.isEmpty(list.get(i).getActualSendNumber()) && Float.parseFloat(list.get(i).getActualSendNumber()) > 0) {
                CYChangeStorageConfirmRequestBean.ItemListBean itemListBean = new CYChangeStorageConfirmRequestBean.ItemListBean();
                LogUtils.i("Purchase_order_code ==== " + list.get(i).getPurchase_order_code());
                itemListBean.setPurchase_order_code(list.get(i).getPurchase_order_code());
                itemListBean.setSupplier_code(list.get(i).getSupplier_code());
                itemListBean.setSupplier_name(list.get(i).getSupplier_name());
                itemListBean.setOrder_qty(list.get(i).getOrder_qty());
                itemListBean.setMat_id(list.get(i).getMat_id());
                itemListBean.setUnit_id(list.get(i).getUnit_id());
                List<BatchBean.BatchInfoBean> batchInfoBeanList = list.get(i).getBatchList();
                List<CYChangeStorageConfirmRequestBean.ItemListBean.StockPositionListBean> stockPositionListBeanList = new ArrayList<>();
                for (int j = 0; j < batchInfoBeanList.size(); j++) {
                    CYChangeStorageConfirmRequestBean.ItemListBean.StockPositionListBean positionListBean = new CYChangeStorageConfirmRequestBean.ItemListBean.StockPositionListBean();
                    positionListBean.setStock_id(batchInfoBeanList.get(j).getStock_id());
                    positionListBean.setStock_qty(batchInfoBeanList.get(j).getStock_qty());
                    positionListBean.setTransport_qty(batchInfoBeanList.get(j).getQty());
                    stockPositionListBeanList.add(positionListBean);
                }
                itemListBean.setStock_position_list(stockPositionListBeanList);
                itemList.add(itemListBean);
            }
        }
        bean.setItem_list(itemList);
        return bean;
    }

    //保存转储单id
    public static void updateStockTransportId(Context context, String dumpNumber) {
        CYChangeStorageBean bean = getChangeStorageBean(context);
        bean.setStock_transport_id(dumpNumber);
        saveCYChangeStorageInformation(context, bean);
    }
}
